package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther gwt
 * @date 2021/6/18 22:48
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            month = month + ".31";
            Integer count = memberDao.findMemberCountByMonth(month);
            list.add(count);
        }
        return list;
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> members = memberDao.selectByCondition(queryString);
        long total = members.getTotal();
        List<Member> rows = members.getResult();
//        System.out.println(total);
//        System.out.println(rows);
        return new PageResult(total, rows);
    }


    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void upDate(Member member) {
        memberDao.upDate(member);
    }

    @Override
    public void delete(Integer id) {
        memberDao.delte(id);
    }

    @Override
    public List<Member> findMemberIds(Integer[] memberIds) {
        List<Member> list = new ArrayList<>();
        for (Integer memberId : memberIds) {
            Member member = memberDao.findById(memberId);
            list.add(member);
        }
        return list;
    }
}
